package com.pai4j.connect.websocket.config;
import com.pai4j.connect.websocket.handler.ChatWebSocketEndPointServletHandler;
import com.pai4j.connect.websocket.handler.GroupVideoWebSocketEndPointServletHandler;
import com.pai4j.connect.websocket.handler.P2PVideoWebSocketEndPointServletHandler;
import com.pai4j.connect.websocket.interceptor.ChatWebSocketEndPointServletInterceptor;
import com.pai4j.connect.websocket.interceptor.GroupVideoWebSocketEndPointServletInterceptor;
import com.pai4j.connect.websocket.interceptor.VideoWebSocketEndPointServletInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConnectServletConfig implements WebSocketConfigurer {

    /**
     *  这里的指定路径 CHAT_WEBSOCKET_PATH 是 WebSocket 服务的访问地址，
     *  客户端通过这个路径与服务器建立 WebSocket 连接。
     *
     *  作用：
     * 用于区分不同的 WebSocket 服务端点。在这个例子中，该路径表示聊天场景的 WebSocket 端点。
     * 客户端需使用匹配的 URL 访问此端点（如 ws://<host>:<port>/connect-service/ws/chat/123/456）。
     * 支持动态路径参数，便于根据业务需求（如不同用户或业务代码）创建独立的连接通道
     *
     * @author: CYM-pai
     * @date: 2025/05/13 17:03
     **/
    
    // 定义聊天WebSocket路径
    private static final String CHAT_WEBSOCKET_PATH = "/connect-service/ws/chat/{bizCode}/{userId}";
    
    
    // 定义点对点视频WebSocket路径
    private static final String P2P_VIDEO_WEBSOCKET_PATH = "/connect-service/ws/p2p/video/{bizCode}/{userId}";
    
    // 定义群组视频WebSocket路径
    private static final String GROUP_VIDEO_WEBSOCKET_PATH = "/connect-service/ws/group/video/{bizCode}/{groupId}/{userId}";
    
    /**
     * 注册WebSocket处理器
     *
     * @param registry WebSocket处理器注册表
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        /**
         * 注册对话场景WS端点服务
         * 该代码将 ChatWebSocketEndPointServletHandler 实例注册为 WebSocket 处理器，
         * 处理路径为 /connect-service/ws/chat/{bizCode}/{userId} 的 WebSocket 请求。
         * addHandler：注册处理器和对应路径
         * setAllowedOrigins("*")：允许所有跨域请求
         * addInterceptors：添加拦截器进行请求拦截处理
         *
         */
        registry.addHandler(chatWebSocketEndPointServletHandler(), CHAT_WEBSOCKET_PATH)
                .setAllowedOrigins("*")
                .addInterceptors(new ChatWebSocketEndPointServletInterceptor());
        /**
         * 注册点对点音视频通话ws信令服务
         */
        registry.addHandler(p2PVideoWebSocketEndPointServletHandler(), P2P_VIDEO_WEBSOCKET_PATH)
                .setAllowedOrigins("*")
                .addInterceptors(new VideoWebSocketEndPointServletInterceptor());
        /**
         * 注册群聊多人音视频通话ws信令服务
         */
        registry.addHandler(groupVideoWebSocketEndPointServletHandler(), GROUP_VIDEO_WEBSOCKET_PATH)
                .setAllowedOrigins("*")
                .addInterceptors(new GroupVideoWebSocketEndPointServletInterceptor());
        
    }
    /**
     * 创建并返回聊天WebSocket端点处理器
     *
     * @return 聊天WebSocket端点处理器实例
     */
    @Bean
    public ChatWebSocketEndPointServletHandler chatWebSocketEndPointServletHandler() {
        return new ChatWebSocketEndPointServletHandler();
    }

    @Bean
    public P2PVideoWebSocketEndPointServletHandler p2PVideoWebSocketEndPointServletHandler() {
        return new P2PVideoWebSocketEndPointServletHandler();
    }

    @Bean
    public GroupVideoWebSocketEndPointServletHandler groupVideoWebSocketEndPointServletHandler() {
        return new GroupVideoWebSocketEndPointServletHandler();
    }
}